var async = require("async");
var commService = require("../service/CommService");

exports.deposInfo = function (req,res,next) {
    async.series({
        comm:function (callbk) {
            commService.getCommSumByState(req.session.openid,callbk);
        },
        deposList:function (callbk) {
            commService.getDeposList(req.session.openid,callbk)
        }
    },function (err,result) {
        if(!err){
            res.render("deposinfo",{title:"提现明细",commValue:result.comm,deposInfo:result.deposList});
        }
    });
}


exports.getComm = function (req,res,next) {
    async.series({
        getComm:function (callbk) {
            var commTotal = 0;
            var commId = [];
            commService.getComm(req.session.openid,function (err,result) {
                if(result.length > 0){
                    for(var i in result){
                        commTotal += result[i].comm_value;
                        commId.push(result[i].comm_id);
                    }
                }
                commService.insDeposInfo(req.session.openid,commTotal,0,commId.join(","),req.query.payMethod,req.query.phone,function (err,rest) {
                    if(rest.affectedRows > 0){
                        commService.updCommState(commId.join(","),callbk);
                    }
                })
            })
        }
    },function (err,result) {
        var p = {
            code:0,
            msg:"申请成功"
        };
        res.status(200).end(JSON.stringify(p));
    });
}